<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\ProductRepository;
use App\Repositories\CategoryRepository;
use App\Traits\BaseFun;

class ProductController extends Controller
{
	public function __construct(ProductRepository $ProductRepository,CategoryRepository $CategoryRepository)
    {
        $this->ProductRepository=$ProductRepository;
        $this->CategoryRepository=$CategoryRepository;
    }
    public function Product(Request $request)
    {   
        $where=['tc' =>2];
        if ($request->search) {
            $where['search']=$request->search;
        }
        if ($request->category) {
            $where['category']=$request->category;
        }
        if ($request->zixuan) {
            $where['zixuan']=$request->zixuan;
        }
        $res = $this->ProductRepository->pageList($where);
        foreach ($res as $key => $value) {
            $res[$key]['cateName']=$this->CategoryRepository->info(['id'=>$value['category']])['name'];
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductAdd(Request $request)
    {   
        $res = $this->ProductRepository->create($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '添加失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductEdit(Request $request,$id)
    {
        $res=$this->ProductRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductUpdate(Request $request)
    {
        $res=$this->ProductRepository->update($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改成功!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductDel(Request $request,$id)
    {
        $res=$this->ProductRepository->delete($id);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductState(Request $request, $id)
    {
        $info = $this->ProductRepository->info(['id' => $id]);
        if ($info['state'] == 1) {
            # 封号 删除 token
            $param = ['state' => '2'];
        } elseif ($info['state'] == 2) {
            $param = ['state' => '1'];
        }
        $param['id'] = $id;

        $up = $this->ProductRepository->update($param);
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '关闭或开启失败'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }
    /*批量下架*/
    public function ProductPlxj(Request $request)
    {
        $param=[];
        $param['state']=2;
        $param['ids']=explode(',', $request->ids);
        $up = $this->ProductRepository->stateUpdate($param);
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '关闭或开启失败'];
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    /*批量上架*/
    public function ProductPlsj(Request $request)
    {
        $param=[];
        $param['state']=1;
        $param['ids']=explode(',', $request->ids);
        $up = $this->ProductRepository->stateUpdate($param);
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '关闭或开启失败'];
        }
        return BaseFun::jsonReturn($ArrDate);
    }

}
